package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.service.DishDtoService;
import com.itheima.reggie.service.DishFlavorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishDtoController {

    @Autowired(required = false)
    private DishDtoService dishDtoService;
    @Autowired(required = false)
    private DishFlavorService dishFlavorService;
//    @Autowired
//    private CategoryService categoryService;


    /**
     * 新增菜品
     * @param dishDto
     * @param session
     * @return
     */
    @PostMapping
    public R<String> add(@RequestBody DishDto dishDto, HttpSession session){
//        //获取菜品分类
//        Category category = categoryService.selectByName(dishDto.getCategoryName());
//        dishDto.setCategoryId(category.getId());

        //设置排序
        dishDto.setSort(1);
        dishDto.setStatus(1);
        dishDto.setCreateTime(LocalDateTime.now());
        dishDto.setUpdateTime(LocalDateTime.now());
        //获取登录管理员id
        Long id = (Long) session.getAttribute("employee");
        //设置创建人
        dishDto.setCreateUser(id);
        dishDto.setUpdateUser(id);
        //调用service添加菜品
        dishDtoService.addDishDto(dishDto);


        //2.添加口味
        List<DishFlavor> dishFlavorList = dishDto.getFlavors();
        //获取登录信息
        Long loginId = (Long) session.getAttribute("employee");
        //获取菜品id,通过名字查询到已插入的菜品，获取id赋值给口味
        Long dishId = dishDtoService.selectByName(dishDto.getName()).getId();
        //补全口味信息
        for (DishFlavor dishFlavor : dishFlavorList) {
            //关联菜品id
            dishFlavor.setDishId(dishId);
            //设置创建和修改时间
            dishFlavor.setCreateTime(LocalDateTime.now());
            dishFlavor.setUpdateTime(LocalDateTime.now());
            //设置创建和修改的人员id
            dishFlavor.setCreateUser(id);
            dishFlavor.setUpdateUser(id);
        }
        dishFlavorService.add(dishFlavorList);
         return R.success("");
    }

    /** 
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<DishDto>> findAllByName(Integer page,Integer pageSize,String name){
        Page<DishDto> dishPage = dishDtoService.findByPage(page, pageSize, name);
        return R.success(dishPage);
    }


    /**
     * 根据id查找dishDto,回显数据
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}")
    public R<DishDto> findDishDtoById(@PathVariable Long id){
        DishDto dishDto = dishDtoService.findDishDtoById(id);
        return R.success(dishDto);
    }

    /**
     * 修改菜品
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        dishDtoService.update(dishDto);
        return R.success("");
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deleteByIds(Long[] ids){
        dishDtoService.deleteByIds(ids);
        return R.success("");
    }

    /**
     * 批量禁用
     * @param ids
     * @return
     */
    @PostMapping(value = "/status/0")
    public R<String> unUse(Long[] ids){
        dishDtoService.unUse(ids);
        return R.success("");
    }

    /**
     * 批量启用
     * @param ids
     * @return
     */
    @PostMapping(value = "/status/1")
    public R<String> use(Long[] ids){
        dishDtoService.use(ids);
        return R.success("");
    }


    /**
     * 根据菜品分类id查找所有菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public R<List<DishDto>> findDishDtoByCategoryId(Long categoryId,Integer status){
        List<DishDto> dishDtoList = dishDtoService.findDishDtoByCategoryId(categoryId,status);
        return R.success(dishDtoList);


    }

}
